package com.hcbd.tdwt.ui.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.ui.common.adapter.SelectBottomAdapter;


import java.util.List;
public class BottomSelectDialog  extends Dialog {

    private RecyclerView contentRV;
    private Button cancelBtn;
    private Context context;
    private List<String> strList;
    private View line;
    private SelectBottomAdapter selectBottomAdapter;
    private TextView titleTv;
    private String title;
    private int type;
    public BottomSelectDialog(Context context,String title,List<String> textList, OnItemClickListener onItemClickListener) {
        super(context);
        this.context = context;
        this.onItemClickListener = onItemClickListener;
        this.strList=textList;
        this.title=title;
    }
    public BottomSelectDialog(Context context,String title,List<String> textList, OnItemClickListener onItemClickListener,int type) {
        super(context);
        this.context = context;
        this.onItemClickListener = onItemClickListener;
        this.strList=textList;
        this.title=title;
        this.type=type;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_bottom_select);
        // 设置触摸外部界面使dialog消失
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = dm.widthPixels;
        window.setAttributes(lp);
        contentRV = findViewById(R.id.contentRV);
        cancelBtn = findViewById(R.id.cancel_btn);
        line=findViewById(R.id.line);
        titleTv = findViewById(R.id.title);
        titleTv.setText(title);

        init();
        setBgStyle();
    }
    public void setBgStyle(){
        if(BdToolFactory.app_type== BdToolFactory.FU_DA_BG_TYPE||BdToolFactory.app_type== BdToolFactory.HAI_GE_BG_TYPE||BdToolFactory.app_type== BdToolFactory.HLCT_BG_TYPE){
            titleTv.setTextColor(context.getColor(R.color.black));
            titleTv.setBackground(context.getDrawable(R.color.white));
            cancelBtn.setTextColor(context.getColor(R.color.black));
            cancelBtn.setBackground(context.getDrawable(R.color.white));
            line.setVisibility(View.GONE);
        }
    }
    public void initAdapter(){
        if(selectBottomAdapter==null){
            selectBottomAdapter = new SelectBottomAdapter(context, strList,type);
            selectBottomAdapter.setHasStableIds(true);
        }else {
            selectBottomAdapter.notifyDataSetChanged(strList);
        }
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 1);
        contentRV.setLayoutManager(gridLayoutManager);
        contentRV.setAdapter(selectBottomAdapter);
        selectBottomAdapter.setOnItemClickListener((index, str) -> {
            onItemClickListener.onSelect(index,str);
            dismiss();
        });
    }
    public void init() {
        initAdapter();
        cancelBtn.setOnClickListener(view -> dismiss());
    }

    public static void show(Dialog dialog) {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public static void shutDown(Dialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();

    }

    private OnItemClickListener onItemClickListener;


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onSelect(int index,String str);
    }
}